package kr.saeildamil.androidforstudy2.ch31;

import kr.saeildamil.androidforstudy2.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.TextView;

public class GestureNavi extends Activity {
	TextView mResult;
	TextView mTextCount;

	int mCount = 10;
	GestureDetector mDetector;

	final static int DISTANCE = 150;
	final static int VELOCITY = 200;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gesturenavi);

		mResult = (TextView) findViewById(R.id.result);
		mTextCount = (TextView) findViewById(R.id.count);

		mDetector = new GestureDetector(this, mGestureListener);
		mDetector.setIsLongpressEnabled(false);
		mTextCount.setText(Integer.toString(mCount));

	}

	OnGestureListener mGestureListener = new OnGestureListener() {

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if (Math.abs(velocityX) > VELOCITY) {
				if (e1.getX() - e2.getX() > DISTANCE || e1.getY() - e2.getY() > DISTANCE) {
					mCount--;
				}
				if (e2.getX() - e1.getX() > DISTANCE || e2.getY() - e1.getY() > DISTANCE) {
					mCount++;
				}
				mTextCount.setText(Integer.toString(mCount));
				String text = String.format("Distance : %d, Velocity : %d VelocityX : %d, VelocityY : %d", 
						(int)Math.abs(e1.getX() - e2.getX()), (int)Math.abs(e1.getY() - Math.abs(e2.getY())), (int)velocityX, (int)velocityY); 
				mResult.setText(text);
			}
			return true;
		}

		@Override
		public boolean onDown(MotionEvent e) {
			return false;
		}
	};
	
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	}
}
